$(function(){
//===============	上传2张照片以上  ==========================
	$(".upload-pic").on("change","input[type='file']",function(){
		var This=$(this).closest(".upload-pic").children(".pre-box")
		var r = new FileReader();
		var file = this.files[0];
		r.readAsDataURL(file);
		r.onload=function(event) {
			This.prepend('<div class="pre-img fl"><img src="'+event.target.result+'" alt="" /></div>')
			if($(".upload-pic .pre-img").length<2){
				$(".upload-pic .add-btn").prepend('<input type="file" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" data-require="*" data-tip="请上传至少2张照片" name="images[]">')
			}else{
				$(".upload-pic .add-btn").prepend('<input type="file" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" name="images[]">')
			}	
		};
				
	})
	$(".upload-pic .pre-box").on("click",".pre-img",function(){ //点击删除
		if($(".upload-pic .pre-box .pre-img").length<2){
			alert('最后一个不能删除！');return;
		}
		$(this).remove()
		$(".upload-pic .add-btn input[type='file']").eq($(this).index()).remove()
		$(".upload-pic .add-btn input[type='text']").eq($(this).index()).remove()
		if($(".upload-pic .pre-box .pre-img").length<2){
			$(".upload-pic .add-btn input[type='file']:first-child").attr("data-require","*")
			$(".upload-pic .add-btn input[type='file']:first-child").attr("data-tip","请上传照片")
		}
	})
//===============	上传2张照片以上  end ==========================
//===============	上传资质  ==========================
	$(".upload-zizhi").on("change","input[type='file']",function(){
		var This=$(this).closest(".upload-zizhi").children(".pre-box")
		var r = new FileReader();
		var file = this.files[0];
		r.readAsDataURL(file);
		r.onload=function(event) {
			This.prepend('<div class="pre-img fl"><img src="'+event.target.result+'" alt="" /></div>')
			$(".upload-zizhi .add-btn").prepend('<input type="file" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" name="zhengshu[]">')
		};
				
	})
	$(".upload-zizhi .pre-box").on("click",".pre-img",function(){ //点击删除
		$(this).remove();
		$(".upload-zizhi .add-btn input[type='file']").eq($(this).index()).remove();
		$(".upload-zizhi .add-btn input[type='text']").eq($(this).index()).remove();
	})
//===============	上传资质  end ==========================
});